package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntTldjkzdsyjlBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntTldjkzdsyjlBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntTldjkzdsyjlBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntTldjkzdsyjlBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntTldjkzdsyjlBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntTldjkzdsyjlBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntTldjkzdsyjlBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 混凝土坍落度及扩展度经时损失试验检测记录Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntTldjkzdsyjlBsInfoServiceImpl implements IReportSnhntTldjkzdsyjlBsInfoService {

    private final ReportSnhntTldjkzdsyjlBsInfoMapper baseMapper;
    private final ReportSnhntTldjkzdsyjlBaDetailMapper detailMapper;

    /**
     * 查询混凝土坍落度及扩展度经时损失试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntTldjkzdsyjlBsInfoVo queryById(Long excelId) {
        ReportSnhntTldjkzdsyjlBsInfoVo vo = baseMapper.selectVoById(excelId);
        LambdaQueryWrapper<ReportSnhntTldjkzdsyjlBaDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReportSnhntTldjkzdsyjlBaDetail::getExcelId, excelId);
        List<ReportSnhntTldjkzdsyjlBaDetail> detailList = detailMapper.selectList(wrapper);
        if (CollUtil.isNotEmpty(detailList)) {
            vo.setDetailList(detailList);
        }
        return vo;
    }

    /**
     * 查询混凝土坍落度及扩展度经时损失试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSnhntTldjkzdsyjlBsInfoVo> queryPageList(ReportSnhntTldjkzdsyjlBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntTldjkzdsyjlBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntTldjkzdsyjlBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询混凝土坍落度及扩展度经时损失试验检测记录列表
     */
    @Override
    public List<ReportSnhntTldjkzdsyjlBsInfoVo> queryList(ReportSnhntTldjkzdsyjlBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntTldjkzdsyjlBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntTldjkzdsyjlBsInfo> buildQueryWrapper(ReportSnhntTldjkzdsyjlBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntTldjkzdsyjlBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntTldjkzdsyjlBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntTldjkzdsyjlBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntTldjkzdsyjlBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSnhntTldjkzdsyjlBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntTldjkzdsyjlBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntTldjkzdsyjlBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntTldjkzdsyjlBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntTldjkzdsyjlBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntTldjkzdsyjlBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportSnhntTldjkzdsyjlBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportSnhntTldjkzdsyjlBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getLaiysj() != null, ReportSnhntTldjkzdsyjlBsInfo::getLaiysj, bo.getLaiysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSnhntTldjkzdsyjlBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytj()), ReportSnhntTldjkzdsyjlBsInfo::getShiytj, bo.getShiytj());
        lqw.eq(StringUtils.isNotBlank(bo.getWend()), ReportSnhntTldjkzdsyjlBsInfo::getWend, bo.getWend());
        lqw.eq(StringUtils.isNotBlank(bo.getXiangdsd()), ReportSnhntTldjkzdsyjlBsInfo::getXiangdsd, bo.getXiangdsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntTldjkzdsyjlBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntTldjkzdsyjlBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntTldjkzdsyjlBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntTldjkzdsyjlBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getHunntzl()), ReportSnhntTldjkzdsyjlBsInfo::getHunntzl, bo.getHunntzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiaobfs()), ReportSnhntTldjkzdsyjlBsInfo::getJiaobfs, bo.getJiaobfs());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntTldjkzdsyjlBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntTldjkzdsyjlBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntTldjkzdsyjlBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntTldjkzdsyjlBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntTldjkzdsyjlBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getHanslpjz() != null, ReportSnhntTldjkzdsyjlBsInfo::getHanslpjz, bo.getHanslpjz());
        return lqw;
    }

    /**
     * 新增混凝土坍落度及扩展度经时损失试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportSnhntTldjkzdsyjlBsInfoBo bo) {
        ReportSnhntTldjkzdsyjlBsInfo add = BeanUtil.toBean(bo, ReportSnhntTldjkzdsyjlBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            //插入子表数据，4行
            List<ReportSnhntTldjkzdsyjlBaDetail> detailList = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                ReportSnhntTldjkzdsyjlBaDetail detail = new ReportSnhntTldjkzdsyjlBaDetail();
                detail.setExcelId(bo.getExcelId());
                detailList.add(detail);
            }
            detailMapper.insertBatch(detailList);
        }
        return flag;
    }

    /**
     * 修改混凝土坍落度及扩展度经时损失试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportSnhntTldjkzdsyjlBsInfoBo bo) {
        ReportSnhntTldjkzdsyjlBsInfo update = BeanUtil.toBean(bo, ReportSnhntTldjkzdsyjlBsInfo.class);
        validEntityBeforeSave(update);
        //修改子表数据
        List<ReportSnhntTldjkzdsyjlBaDetail> detailList = bo.getDetailList();
        if (detailList != null && detailList.size() != 0) {
            detailMapper.updateBatchById(detailList);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntTldjkzdsyjlBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除混凝土坍落度及扩展度经时损失试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
